/*
 * This file is part of aion-unique <aion-unique.org>.
 *
 *  aion-unique is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  aion-unique is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with aion-unique.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.controllers.effect;

import com.aionengine.gameserver.configs.main.CustomConfig;
import com.aionengine.gameserver.dataholders.DataManager;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.network.aion.serverpackets.SM_ABNORMAL_STATE;
import com.aionengine.gameserver.network.aion.serverpackets.SM_PLAYER_STANCE;
import com.aionengine.gameserver.skillengine.model.Effect;
import com.aionengine.gameserver.skillengine.model.SkillTargetSlot;
import com.aionengine.gameserver.skillengine.model.SkillTemplate;
import com.aionengine.gameserver.taskmanager.tasks.PacketBroadcaster.BroadcastMode;
import com.aionengine.gameserver.taskmanager.tasks.TeamEffectUpdater;
import com.aionengine.gameserver.utils.PacketSendUtility;

import java.util.Collection;
import java.util.Collections;

/**
 * @author ATracer
 */
public class PlayerEffectController extends EffectController {

    public PlayerEffectController(Creature owner) {
        super(owner);
    }

    @Override
    public void addEffect(Effect effect) {
        if (checkDuelCondition(effect) && !effect.getIsForcedEffect())
            return;

        super.addEffect(effect);
        updatePlayerIconsAndGroup(effect);
    }

    @Override
    public void clearEffect(Effect effect) {
        super.clearEffect(effect);
        updatePlayerIconsAndGroup(effect);
    }

    @Override
    public Player getOwner() {
        return (Player) super.getOwner();
    }

    /**
     * @param effect
     */
    private void updatePlayerIconsAndGroup(Effect effect) {
        if (!effect.isPassive()) {
            updatePlayerEffectIcons();
            if (getOwner().isInTeam()) {
                TeamEffectUpdater.getInstance().startTask(getOwner());
            }
        }
    }

    @Override
    public void updatePlayerEffectIcons() {
        getOwner().addPacketBroadcastMask(BroadcastMode.UPDATE_PLAYER_EFFECT_ICONS);
    }

    @Override
    public void updatePlayerEffectIconsImpl() {
        Collection<Effect> effects = getAbnormalEffectsToShow();
        PacketSendUtility.sendPacket((Player) getOwner(), new SM_ABNORMAL_STATE(effects, abnormals));
    }

    /**
     * Effect of DEBUFF should not be added if duel ended (friendly unit)
     *
     * @param effect
     * @return
     */
    private boolean checkDuelCondition(Effect effect) {
        Creature creature = effect.getEffector();
        if (creature instanceof Player) {
            if (!getOwner().isEnemy(creature) && effect.getTargetSlot() == SkillTargetSlot.DEBUFF.ordinal()) {
                return true;
            }
        }

        return false;
    }

    /**
     * @param skillId
     * @param skillLvl
     * @param currentTime
     * @param reuseDelay
     */
    public void addSavedEffect(int skillId, int skillLvl, int remainingTime, long endTime) {
        SkillTemplate template = DataManager.SKILL_DATA.getSkillTemplate(skillId);

        if (remainingTime <= 0)
            return;
        if (CustomConfig.ABYSSXFORM_LOGOUT
                && template.isDeityAvatar()) {

            if (System.currentTimeMillis() >= endTime)
                return;
            else
                remainingTime = (int) (endTime - System.currentTimeMillis());
        }

        Effect effect = new Effect(getOwner(), getOwner(), template, skillLvl, remainingTime);
        abnormalEffectMap.put(effect.getStack(), effect);
        effect.addAllEffectToSucess();
        effect.startEffect(true);

        if (effect.getSkillTemplate().getTargetSlot() != SkillTargetSlot.NOSHOW)
            PacketSendUtility.sendPacket(getOwner(), new SM_ABNORMAL_STATE(Collections.singletonList(effect), abnormals));

    }

    @Override
    public void broadCastEffectsImp() {
        super.broadCastEffectsImp();
        Player player = getOwner();
        if (player.getController().isUnderStance()) {
            PacketSendUtility.sendPacket(player, new SM_PLAYER_STANCE(player, 1));
        }
    }

}
